﻿using System.Collections.Generic;
using System.Linq;

namespace Liphofra.Mvvm.Validation
{
    /// <summary>
    /// The result of a validation.
    /// </summary>
    public sealed class ValidationResult
    {
        private static readonly ValidationResult _success = new ValidationResult();
        private readonly List<string> _errors;

        /// <summary>
        /// Gets the static validation result that indicates successful validation.
        /// </summary>
        public static ValidationResult Success
        {
            get
            {
                return _success;
            }
        }

        /// <summary>
        /// Gets the list of errors that happened during validation.
        /// </summary>
        public IEnumerable<string> Errors
        {
            get
            {
                return _errors;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance has errors.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance has errors; otherwise, <c>false</c>.
        /// </value>
        public bool HasErrors
        {
            get
            {
                return Errors != null && Errors.Any();
            }
        }

        /// <summary>
        /// Gets the name of the property the validation result affects.
        /// Can be <c>null</c> to indicate the result is not specific to a single property
        /// or applies to the validated object as a whole.
        /// </summary>
        /// <value>
        /// The name of the property.
        /// </value>
        public string PropertyName
        {
            get;
            private set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationResult" /> class.
        /// </summary>
        public ValidationResult()
        {
            _errors = new List<string>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationResult" /> class.
        /// </summary>
        /// <param name="propertyName">Name of the property the result applies to.</param>
        public ValidationResult(string propertyName)
        {
            PropertyName = propertyName;
            _errors = new List<string>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationResult" /> class.
        /// </summary>
        /// <param name="propertyName">Name of the property the result applies to.</param>
        /// <param name="errors">The errors to initialize the validation result with.</param>
        public ValidationResult(string propertyName, params string[] errors)
        {
            PropertyName = propertyName;
            _errors = new List<string>(errors);
        }

        /// <summary>
        /// Adds a new error to the internal collection of errors.
        /// </summary>
        /// <param name="error">The error to add.</param>
        public void AddError(string error)
        {
            _errors.Add(error);
        }
    }
}